package com.dnjn.happiness.biz.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.biz.domain.TrainingTags;
import com.dnjn.happiness.biz.service.ITrainingTagsService;
import com.dnjn.happiness.common.core.utils.StringUtils;
import com.dnjn.happiness.common.core.utils.poi.ExcelUtil;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Amber
 * @since 2023-10-04
 */
@Tag(name = "admin biz TrainingTags ", description = "")
@RestController
@RequestMapping("/admin/trainingTags")
public class AdminTrainingTagsController extends BaseController {

    @Autowired
    private ITrainingTagsService baseService;

    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable Long id) {
        return success(baseService.getById(id));
    }

    @RequiresPermissions("biz:trainingTags:add")
    @PostMapping("/add")
    @Operation(summary = "后台-培训类型添加")
    public AjaxResult add(@Validated @RequestBody TrainingTags domain) {
        domain.setCreateBy(SecurityUtils.getUsername());
        TrainingTags one = baseService.queryByLabel(domain.getLabel());

        if (ObjectUtils.isNotNull(one)) {
            AjaxResult ajaxResult = new AjaxResult();
            ajaxResult.put("msg", "类型名不能重复");
            return ajaxResult;
        }
        return toAjax(baseService.save(domain));
    }

    @RequiresPermissions("biz:trainingTags:edit")
    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody TrainingTags domain) {
        return toAjax(baseService.updateById(domain));
    }

    @RequiresPermissions("biz:trainingTags:remove")
    @DeleteMapping("/{pkIds}")
    public AjaxResult remove(@PathVariable Long pkIds) {

        boolean update = baseService.removeDomain(pkIds);

        if (update) {
            return success();
        }
        return error("删除失败");

    }

    @RequiresPermissions("biz:trainingTags:query")
    @PostMapping("/page")
    public AjaxResult page(@RequestBody TrainingTags domain, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size);
        //注意前端处理一下 label不是""
        Page<TrainingTags> list = baseService.page(page, new QueryWrapper<TrainingTags>()
                .like(StringUtils.isNotEmpty(domain.getLabel()), "label", domain.getLabel())
                .eq("deleted", "0"));



        return success(list);
    }

    @RequiresPermissions("biz:trainingTags:query")
    @PostMapping("/listTags")
    public AjaxResult listTags() {
        List<TrainingTags> list = baseService.queryListTags();


        return success(list);
    }

    @RequiresPermissions("biz:trainingTags:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainingTags domain) {
        List<TrainingTags> list = baseService.list(Wrappers.query(domain));
        ExcelUtil<TrainingTags> util = new ExcelUtil<TrainingTags>(TrainingTags.class);
        util.exportExcel(response, list, "参数数据");
    }


}